package com.lxg.controller.axiosController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxg.pojo.Books;
import com.lxg.service.booksService.BooksShowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
public class axiosController {
    @Autowired
    BooksShowService booksShowService;


    /*@RequestMapping("/searchBooks")
    public List<Books> searchBooks(Model model,HttpSession session, HttpServletRequest request, @RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize){

        System.out.println("test");

        String searchText = request.getParameter("searchText");
        request.setAttribute("searchText",searchText);

        PageHelper.startPage(pageNum,pageSize);
        ArrayList<Books> searchNameBooks = booksShowService.getAllLikeBooksByName(searchText);


        ArrayList<Books> searchBooks = searchNameBooks;
        if (searchNameBooks.size()==0){
            PageHelper.startPage(pageNum,pageSize);
            ArrayList<Books> searchAuthorBooks = booksShowService.getAllLikeBooksByAuthor(searchText);
            searchBooks = searchAuthorBooks;
        }
        PageInfo<Books> pageInfo = new PageInfo<>(searchBooks,3);
        List<Books> list = pageInfo.getList();
        model.addAttribute("pageInfo",pageInfo);
        return list;
    }
*/


    //axios更新数据
    @RequestMapping("/searchBooks")
    public PageInfo<Books> searchBooks(Model model, HttpSession session, HttpServletRequest request, @RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize){



        String searchText = request.getParameter("searchText");

        System.out.println("searchText:"+searchText);


        PageInfo<Books> pageInfo = null;

        if (searchText=="" || searchText==null){
            System.out.println("全部");
            PageHelper.startPage(pageNum,pageSize);
            ArrayList<Books> allBooks = booksShowService.getAllBooks();
            pageInfo = new PageInfo<>(allBooks,3);
            System.out.println(pageInfo.getList().size());
//            model.addAttribute("pageInfo",pageInfo);
            return pageInfo;
        }else{

            System.out.println("test:"+searchText);

            request.setAttribute("searchText",searchText);



            PageHelper.startPage(pageNum,pageSize);
            ArrayList<Books> searchNameBooks = booksShowService.getAllLikeBooksByName(searchText);
            ArrayList<Books> searchBooks = searchNameBooks;

            if (searchNameBooks.size()==0){

                PageHelper.startPage(pageNum,pageSize);
                ArrayList<Books> searchAuthorBooks = booksShowService.getAllLikeBooksByAuthor(searchText);
                searchBooks = searchAuthorBooks;
            }
            pageInfo = new PageInfo<>(searchBooks,3);
            System.out.println(pageInfo.getList().size());
//            model.addAttribute("pageInfo",pageInfo);
            return pageInfo;
        }
    }






}



